package com.project.schoolFoodShare.controller.remark;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.schoolFoodShare.common.enums.BusinessType;
import com.project.schoolFoodShare.common.enums.ResultCode;
import com.project.schoolFoodShare.domain.FoodRemark;
import com.project.schoolFoodShare.domain.Result;
import com.project.schoolFoodShare.service.FoodRemarkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @version 1.0
 * @description: 说明信息controller
 */
@Controller
@ResponseBody
@RequestMapping("remark")
public class FoodRemarkController {

    @Autowired
    private FoodRemarkService foodRemarkService;

    /** 分页获取说明信息 */
    @PostMapping("getFoodRemarkPage")
    public Result getFoodRemarkPage(@RequestBody FoodRemark foodRemark) {
        Page<FoodRemark> page = new Page<>(foodRemark.getPageNumber(),foodRemark.getPageSize());
        QueryWrapper<FoodRemark> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(foodRemark.getTitle()),FoodRemark::getTitle,foodRemark.getTitle())
                .eq(StringUtils.isNotBlank(foodRemark.getEmail()),FoodRemark::getEmail,foodRemark.getEmail())
                .eq(StringUtils.isNotBlank(foodRemark.getTel()),FoodRemark::getTel,foodRemark.getTel())
                .eq(StringUtils.isNotBlank(foodRemark.getAuthor()),FoodRemark::getAuthor,foodRemark.getAuthor())
                .eq(StringUtils.isNotBlank(foodRemark.getIntroduce()),FoodRemark::getIntroduce,foodRemark.getIntroduce());
        Page<FoodRemark> foodRemarkPage = foodRemarkService.page(page, queryWrapper);
        return Result.success(foodRemarkPage);
    }

    @GetMapping("getFoodRemarkByOne")
    public Result getFoodRemarkByOne() {
        FoodRemark remark = foodRemarkService.getOne(null);
        return Result.success(remark);
    }

    /** 根据id获取说明信息 */
    @GetMapping("getFoodRemarkById")
    public Result getFoodRemarkById(@RequestParam("id")String id) {
        FoodRemark foodRemark = foodRemarkService.getById(id);
        return Result.success(foodRemark);
    }

    /** 保存说明信息 */
    @PostMapping("saveFoodRemark")
    public Result saveFoodRemark(@RequestBody FoodRemark foodRemark) {
        boolean save = foodRemarkService.save(foodRemark);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑说明信息 */
    @PostMapping("editFoodRemark")
    public Result editFoodRemark(@RequestBody FoodRemark foodRemark) {
        boolean save = foodRemarkService.updateById(foodRemark);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除说明信息 */
    @GetMapping("removeFoodRemark")
    public Result removeFoodRemark(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                foodRemarkService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("说明信息id不能为空！");
        }
    }

}
